import { userInfo } from '@/apis/user/personal'
import router from '@/router'
const user = {
    namespaced: true,
    state: {
        token: '',
        userInfo: {},
        userName: ''
    },
    // getters 依赖于 state中原始数据的变化，并返回计算后的新数据
    getters: {
        // 计算属性的方式
        islogin(state) {
            return !!state.token || !!localStorage.getItem('token')
        },
    },
    mutations: {
        setToken(state, res) {
            localStorage.setItem('token', res.token)
            localStorage.setItem('userName', res.account)
            state.token = res.token
            state.userName = res.account
        },
        clearToken(state) {
            localStorage.removeItem('token')
            state.token = ''

        },
        setUserInfo(state, info) {
            state.userInfo = info || {}
        },
        clearUserInfo(state) {
            state.userInfo = {}
            state.account = ''
            localStorage.removeItem("userName")
        },
    },
    actions: {
        refreshInfo({ state, commit, }) {
            userInfo().then((res) => {
                commit('setUserInfo', res.userInfo)
                router.push('/app')
            }).catch(e => {
                console.log(e);
            })
        },
    },
}
export default user